package business_opportunity

import (
	"git.mycaigou.com/gfyx/common/authorize"
	supplierDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/dto/supplier"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/supplier"
	reportService "git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/clue/report"
	"git.myscrm.cn/golang/stark/v4"

	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	"github.com/gin-gonic/gin"
)

type SupplierController struct {
	controller.BaseController
	supplierService supplier.SupplierServiceIface
	reportService   reportService.ReportServiceIface
}

// NewSupplierController
func NewSupplierController(
	supplierService supplier.SupplierServiceIface,
	reportService reportService.ReportServiceIface,
) *SupplierController {
	return &SupplierController{
		supplierService: supplierService,
		reportService:   reportService,
	}
}

// RegisterRouter
// 注册路由
func (c *SupplierController) RegisterRouter(g *gin.RouterGroup) {

	//暂不下线，前端还没有改造完
	g.POST("/check-supplier-main-data", c.CheckSupplierMainData)

}

func (c *SupplierController) CheckSupplierMainData(ctx *gin.Context) {
	c.SetAnonymityUser(ctx)
	request := &supplierDto.CheckSupplierMainDataRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Error(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}
	rs, err := c.supplierService.GetSupplierMainDataMultiName(ctx, request.SupplierName)
	if err != nil {
		stark.Logger.Error(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, rs)
}

func (c *SupplierController) SetAnonymityUser(ctx *gin.Context) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		urlUid := ctx.Request.Header.Get("url_uid")
		c.reportService.SetSharerCodeWithReportUrl(ctx, urlUid)
	}
}
